<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );
jimport( 'joomla.utilities.date' );
jimport( 'joomla.filesystem.file' );
jimport( 'joomla.filesystem.folder' );


class jzmluvyModelUpravazaznamu extends Jmodel
{

  var $_data = null;
  var $_id = null;
  
	function __construct(){
    
    $this->params	= &JComponentHelper::getParams('com_jzmluvy');
//    $post = JRequest::get( 'post' );
    $id = JRequest::getVar( 'id', '', '', 'array' );
		$this->setId($id[0]);
    $this->nastavenie = & jzmluvyHelper::nastavenie();
    		
		parent::__construct();
	}

	function setId($id)
	{
		// Set event id and wipe data
		$this->_id	    = $id;
		$this->_data	= null;
	}

	function &getData()
	{
		if ($this->_loadData())
		{

		}
		else  $this->_initData();

		return $this->_data;
	}

	function _loadData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = 'SELECT *'
					. ' FROM #__jzmluvy'
					. ' WHERE id = '.$this->_id
					;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();

			return (boolean) $this->_data;
		}
		return true;
	}
	
		function _initData()
	{
		
    // Lets load the content if it doesn't already exist
		if (empty($this->_data) && $this->_id == '')
		{
      $now = JFactory::getDate();
			$end = JFactory::getDate();
      $end->_date += (60*60*24*$this->params->get( 'doba_vyveseni', ''));  
			
      $item = new stdClass();
			$item->id					= 0;
			$item->catid					= 0;
			$item->title				= '';
			$item->alias				= '';
			$item->created_by_alias		= '';
			$item->fulltext				= '';
			$item->jedcislo				= '';
			$item->publish_up				= $now->toFormat('%Y-%m-%d');
			$item->publish_down				= $end->toFormat('%Y-%m-%d');
			$item->vlastni1				= '';
			$item->vlastni2				= '';
			$this->_data				= $item;
			return (boolean) $this->_data;
		}
		if (empty($this->_data) && $this->_id == 0)
		{
			$post = JRequest::get( 'get' );
      
      $item = new stdClass();
			$item->id					= 0;
			$item->catid					= $post['catid'];
			$item->title				= $post['title'];
			$item->created_by_alias				= $post['created_by_alias'];
			$item->alias				= '';
			$item->fulltext				= '';
			$item->jedcislo				= $post['jedcislo'];
			$item->publish_up				= $post['publish_up'];
			$item->publish_down				= $post['publish_down'];
			$this->_data				= $item;
			return (boolean) $this->_data;
		}
		return true;
	}

	function store($data)
	{
		global $mainframe;

		$user		= & JFactory::getUser();

		$row =& JTable::getInstance('upravazaznamu', '');

		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
/*		
		// Check/sanitize the metatags
		$row->meta_description = htmlspecialchars(trim(addslashes($row->meta_description)));
		if (JString::strlen($row->meta_description) > 255) {
			$row->meta_description = JString::substr($row->meta_description, 0, 254);
		}

		$row->meta_keywords = htmlspecialchars(trim(addslashes($row->meta_keywords)));
		if (JString::strlen($row->meta_keywords) > 200) {
			$row->meta_keywords = JString::substr($row->meta_keywords, 0, 199);
		}

		//Check if image was selected
		jimport('joomla.filesystem.file');
		$format 	= JFile::getExt('JPATH_SITE/images/eventlist/events/'.$row->datimage);

		$allowable 	= array ('gif', 'jpg', 'png');
		if (in_array($format, $allowable)) {
			$row->datimage = $row->datimage;
		} else {
			$row->datimage = '';
		} */

    //kontrola velikosti souboru
    if (!empty($data['file']['name'])) {
      $velikostSouboru = $this->params->get( 'max_velikost', '')*1048576;
      if ($data['file']['size'] > $velikostSouboru && $this->params->get( 'max_velikost', '') != 0) {
        $this->_error = 'Není povoleno nahrávat soubory větší než '.$this->params->get( 'max_velikost', '').' MB. ';
        $this->_error .= 'Váš soubor má velikost '. round($data['file']['size']/1048576,2).' MB. ';
        JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
        return false;
			}
    }

		// sanitise id field
		$row->id = (int) $row->id;

		$nullDate	= $this->_db->getNullDate();

    //uzivatel pridavajici nebo editujici zaznam
		if ($row->id) {
			$row->modified 		= gmdate('Y-m-d H:i:s');
			$row->modified_by 	= $user->get('id');
		} else {
			$row->modified 		= $nullDate;
			$row->modified_by 	= '';

			//get time and userid
			$row->created 			= gmdate('Y-m-d H:i:s');

//			$row->author_ip 		= $elsettings->storeip ? getenv('REMOTE_ADDR') : 'DISABLED';
			$row->created_by		= $user->get('id');
		}
    
    /*
		if (!empty($data['file']['name'])) {
      $row->file = $data['file']['name'];
      
      $row->filename = JFilterOutput::stringURLSafe(JFile::stripExt($data['file']['name']));
      $row->extension = strtolower(JFile::getExt($data['file']['name'])); 
    }
    
    if (!empty($data['soubor'])) {
      $row->file = $data['soubor']; 
    }
    */
		// Make sure the data is valid
		if (!$row->check()) {
			$this->setError($row->getError());
			return false;
		}

		// Store the table to the database
		if (!$row->store(true)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		if (!empty($data['file']['name'])) {
      $soubor = $data['file'];

      $nazevSouboru = JFile::stripExt($soubor);
      $priponaSouboru = JFile::getExt($soubor['name']);
      $nazevSouboru = $row->id.'-'.JFilterOutput::stringURLSafe($nazevSouboru['name']);
      $nazevSouboru .= '.'.JFilterOutput::stringURLSafe($priponaSouboru);
      
      $src = $soubor['tmp_name'];
      $dest = JPATH_ROOT . DS . $this->params->get( 'adresar') . $nazevSouboru;
      JFile::upload($src, $dest);
    }
		
		return $row->id;
	}
}
?>